﻿using System;
using System.Web.UI.WebControls;
using Medianamik.Core;
using Medianamik.Core.CodeGeneration;
using Telerik.Web.UI;

namespace Medianamik.UI.TypeManagement
{
    public partial class ManyToManyTypeList : Medianamik.UI.BasePage
    {
        #region Fields



        #endregion

        #region Events

        protected void Page_Load(object sender, EventArgs e)
        {
            
        }

        protected void ManyToManyTypesGrid_ItemCommand(object sender, GridCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "Delete":
                    //TODO: Vérifier que le type peut être supprimé
                    NodeTypeManager.DeleteNodeType(new Guid(e.CommandArgument.ToString()));
                    ManyToManyTypesGrid.Rebind();
                    break;
                case "Regen":
                    CodeService code = new CodeService();
                    NodeType t = NodeTypeManager.GetNodeType(new Guid(e.CommandArgument.ToString()));
                    code.GenerateTableAndView(t);
                    code.FillTable(t);
                    break;
            }
        }

        protected void ManyToManyTypesGrid_ItemDataBound(object sender, GridItemEventArgs e)
        {
            var lblName = e.Item.FindControl("lblName") as Label;
            if (lblName != null)
            {
                var type = (e.Item.DataItem as NodeType);
                if (type != null)
                {
                    lblName.Text = Resources("typemanagement", type.ID.ToString() + "_Label");
                }
            }
        }

        protected void ManyToManyTypesGrid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
        {
            ManyToManyTypesGrid.DataSource = NodeTypeManager.GetManyToManyTypes();
        }

        #endregion

        #region Properties

        private string _addURL = null;
        public string AddURL
        {
            get
            {
                if (_addURL == null)
                {
                    _addURL = "manytomanytypemanagement.aspx";
                }

                return _addURL;
            }
        }

        #endregion

        #region Methods

        

        #endregion
    }
}
